<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<script type="text/javascript">
		const objModel = [
			{
				"id": "1450518493282832384",
				"name": "中电二所制绒右侧上料盒自动化02#",
				"total": 200,
				"groupNameA": 'A',
				"groupCountA": 100,
				"groupCountB": 100,
				"gn1": '总气压异常',
				"gn2": '堵片报警',
				"gn3": '吸片2位',
				"gn4": '吸片3位',
				"gn5": '校正位漫反射',
				"gn6": '装片位',
				"num1": 1,
				"num2": 881,
				"num3": 1,
				"num4": 2,
				"num5": 3,
				"num6": 4,
			},
			{
				"id": "1450518493282832384",
				"name": "中电二所制绒右侧上料盒自动化02#",
				"alarmTypeName": "报警统计",
				"total": 200,
				"groupNameA": 'B',
				"groupCountA": 100,
				"groupCountB": 100,
				"gn1": '总气压异常',
				"gn2": '堵片报警',
				"gn3": '吸片2位',
				"gn4": '吸片3位',
				"gn5": '校正位漫反射',
				"gn6": '装片位',
				"num1": 1,
				"num2": 881,
				"num3": 1,
				"num4": 2,
				"num5": 3,
				"num6": 4,
			}
		]
	</script>
	<body>
		<script type="text/javascript">
			const alerm = [
				{
					"id": "1450518493282832384",
					"name": "中电二所制绒右侧上料盒自动化02#",
					"alarmType": 5,
					"alarmTypeName": "报警统计",
					"startTime": "2021-07-01T00:00:00+08:00",
					"endTime": "2021-09-02T00:00:00+08:00",
					"datas": [
						{
							"groupName": "A",
							"count": "54",
							"datas": [
								{
									"groupName": "总气压异常",
									"count": "0",
									"datas": []
								},
								{
									"groupName": "堵片报警",
									"count": "881",
									"datas": [
										{
											"groupName": "吸片2位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "吸片3位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "校正位漫反射",
											"count": "20",
											"datas": null
										},
										{
											"groupName": "装片位",
											"count": "0",
											"datas": null
										}
									]
								}
							]
						},
						{
							"groupName": "B",
							"count": "827",
							"datas": [
								{
									"groupName": "总气压异常",
									"count": "1",
									"datas": []
								},
								{
									"groupName": "堵片报警",
									"count": "800",
									"datas": [
										{
											"groupName": "吸片2位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "吸片3位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "校正位漫反射",
											"count": "20",
											"datas": null
										},
										{
											"groupName": "装片位",
											"count": "0",
											"datas": null
										}
									]
								}
							]
						}
					]
				},
				{
					"id": "1445152451891171328",
					"name": "中电二所制绒左侧上料盒自动化07#",
					"alarmType": 5,
					"alarmTypeName": "报警统计",
					"startTime": "2021-07-01T00:00:00+08:00",
					"endTime": "2021-09-02T00:00:00+08:00",
					"datas": [
						{
							"groupName": "A",
							"count": "1243",
							"datas": [
								{
									"groupName": "总气压异常",
									"count": "0",
									"datas": []
								},
								{
									"groupName": "堵片报警",
									"count": "1243",
									"datas": [
										{
											"groupName": "吸片2位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "吸片3位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "校正位漫反射",
											"count": "24",
											"datas": null
										},
										{
											"groupName": "装片位",
											"count": "0",
											"datas": null
										}
									]
								}
							]
						},
						{
							"groupName": "B",
							"count": "0",
							"datas": [
								{
									"groupName": "总气压异常",
									"count": "0",
									"datas": []
								},
								{
									"groupName": "堵片报警",
									"count": "1243",
									"datas": [
										{
											"groupName": "吸片2位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "吸片3位",
											"count": "0",
											"datas": null
										},
										{
											"groupName": "校正位漫反射",
											"count": "24",
											"datas": null
										},
										{
											"groupName": "装片位",
											"count": "0",
											"datas": null
										}
									]
								}
							]
						}
					]
				},
				 {
					"id": "1450519087338885120",
					"name": "中电二所制绒左侧上料盒自动化03#",
					"alarmType": 5,
					"alarmTypeName": "报警统计",
					"startTime": "2021-07-01T00:00:00+08:00",
					"endTime": "2021-09-02T00:00:00+08:00",
					"datas": [{
						"groupName": "A",
						"count": "1587",
						"datas": [
							{
								"groupName": "总气压异常",
								"count": "0",
								"datas": []
							},
							{
								"groupName": "堵片报警",
								"count": "1587",
								"datas": [
									{
										"groupName": "吸片2位",
										"count": "0",
										"datas": null
									},
									{
										"groupName": "吸片3位",
										"count": "0",
										"datas": null
									},
									{
										"groupName": "校正位漫反射",
										"count": "84",
										"datas": null
									},
									{
										"groupName": "装片位",
										"count": "0",
										"datas": null
									}
								]
							}
						]
					}]
				}
			]
			
			// 定义一个数组，处理后的
			const tempArr = []
			for (let i = 0; i < alerm.length; i++) {
				const jLen = alerm[i].datas
				let total = 0;
				for (let j = 0; j< jLen.length; j ++) {
					total += Number(jLen[j].count) // 计算 A + B 侧总合
					alerm[i].total = total
					if(jLen.length > 1) {
						// console.log('A + B 侧和', jLen[j]);
						if(jLen[j].groupName && jLen[j].groupName.includes('A')) {
							alerm[i].totalA = jLen[j].count
							// alerm[i]['groupNameA'] = jLen[j].groupName
						}
						if(jLen[j].groupName && jLen[j].groupName.includes('B')) {
							alerm[i].totalB = jLen[j].count
							// alerm[i]['groupNameB'] = jLen[j].groupName
						}
					} else if(jLen.length > 0){
						// console.log('A + B 侧和', jLen[j]);
						if(jLen[j].groupName && jLen[j].groupName.includes('A')) {
							alerm[i].totalA = jLen[j].count
							alerm[i].totalB = 0
							// alerm[i]['groupNameA'] = jLen[j].groupName
							// alerm[i]['groupNameB'] = ''
						}
						if(jLen[j].groupName && jLen[j].groupName.includes('B')) {
							alerm[i].totalB = jLen[j].count
							alerm[i].totalA = 0
							// alerm[i]['groupNameB'] = jLen[j].groupName
							// alerm[i]['groupNameA'] = ''
						}
					}
					else{
						alerm[i].totalA = 0
						alerm[i].totalB = 0
						// alerm[i]['groupNameA'] = ''
						// alerm[i]['groupNameB'] = ''
						// alerm[i]['groupA'] = []
						// alerm[i]['groupB'] = []
					}
				}
			}
			 // console.log('第1次处理后数据：', alerm);
			 
			// 先判断有没有 A, B侧，如果有A,B侧 需要将一个对象拆分为 A,B 2 个对象
			for (let i = 0; i < alerm.length; i++) {
				// 有 A, B侧
				if(alerm[i].datas.length > 1){
					for (let j = 0; j < alerm[i].datas.length; j++) {
						alerm[i]['id'] = alerm[i]['id'] + '-' + i+j
						tempArr.push({
							groupData: alerm[i].datas[j].datas,
							groupName: alerm[i].datas[j].groupName,
							...alerm[i]
						})
					}
				}
				// 只有一侧
				if(alerm[i].datas.length === 1) {
					alerm[i]['id'] = alerm[i]['id'] + '-' + i
					tempArr.push({
						groupData: alerm[i].datas[0].datas,
						groupName: alerm[i].datas[0].groupName,
						...alerm[i]
					})
				}
			}
			// console.log('第二次处理后数据：', tempArr);
			
			// 第3次处理数据格式
			// 去除一些无用属性
			const deletProperty = ['datas', 'groupData', 'startTime', 'endTime', 'alarmType']
			for (let i = 0; i < tempArr.length; i++) {
				const item = tempArr[i]
				for (let j = 0; j < item.groupData.length; j++) {
					item[`gn-${j}`] = item.groupData[j].groupName
					item[`count-${j}`] = item.groupData[j].count
					item[`cusData${j}`] = item.groupData[j].datas
				}
                for (let key in item) {
                    if (key && key.includes("cusData")) {
                    if (item[key] && item[key].length > 0) {
                        for (let k = 0; k < item[key].length; k++) {
                        // 新增地属性 应该在原来属性索引后面
                        item[`gn-${k + 2}`] = item[key][k].groupName;
                        item[`count-${k + 2}`] = item[key][k].count;
                        }
                    }
                    }
                    if (deletProperty.includes(key) || key.includes("cusData")) {
                    delete item[key];
                    }
                }
			}
			console.table('最终处理后数据：', tempArr);
			
		</script>

        <script type="text/javascript">
			let temp1 = []
			let c1 = []
			let c2 = []
			const dynamicCloumns1 = {
				"Id": "1450518193805332480",
				"Name": "中电二所制绒左侧上料盒自动化02#",
				"Count": "416",
				"Position": "A",
				"PositionCount": "416",
				"Main_0": "吸片2位",
				"Main_0_Count": "1",
				"Main_1": "吸片3位",
				"Main_1_Count": "2",
				"Main_2": "校正位漫反射",
				"Main_2_Count": "3",
				"Main_3": "装片位",
				"Main_3_Count": "4"
			}
			let reg1 = /Main_(\d+)$/
			let reg2 = /_Count$/
			for (let key in dynamicCloumns1) {
				if (reg1.test(key)) {
					c1.push(key)
				}
				if (reg2.test(key)) {
					c2.push(key)
				}
			}
			c1 = [c1]
			for (let i = 0; i < c1.length; i++) {
				for (let j = 0; j < c2.length; j++) {
					temp1.push({
						gname: c2[j],
						gcount: c1[i][j],
						count: dynamicCloumns1[c2[j]]
					})
				}
			}
			console.log('最终处理好的数据：', temp1);
		</script>
	</body>
</html>
